const {
  override,
  overrideDevServer,
  addDecoratorsLegacy,
  disableEsLint,
  addWebpackAlias,
  fixBabelImports,
  addLessLoader,
} = require('customize-cra')
const path = require('path')

// 解决跨域代理配置
const devServerConfig = () => (config) => {
  return {
    ...config,
    // 服务开启gzip
    compress: true,
    proxy: {
      '/api': {
        target: 'xxx',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/api',
        },
      },
    },
  }
}

// 生成环境关闭 sourceMap
const rewiredMap = () => (config) => {
  config.devtool =
    config.mode === 'development' ? 'cheap-module-source-map' : false
  return config
}

module.exports = {
  webpack: override(
    // 添加装饰器
    addDecoratorsLegacy(),
    disableEsLint(),
    // 设置路径别名
    addWebpackAlias({
      '@': path.resolve(__dirname, 'src'),
      '@style': path.resolve(__dirname, 'src/assets/styles'),
    }),
    // 按需加载
    fixBabelImports('import', {
      libraryName: 'antd',
      libraryDirectory: 'es',
      style: true,
    }),
    rewiredMap(),
    // 配置less
    addLessLoader({
      javascriptEnabled: true,
      ModifyVars: { '@primary-color': '#1DA57A' },
      sourceMap: false,
      localIdentName: '[local]--[hash:base64:5]', // 自定义 CSS Modules 的 localIdentName
    })
  ),
  devServer: overrideDevServer(devServerConfig()),
}
